import java.util.HashSet;
import java.util.Set;

/**
 * @Author ZhangCuirong
 * @Date 2025/8/13 11:08
 * @description:
 */
public class Solution202 {
    public int calc(int n) {
        String s = Integer.toString(n);
        int sum = 0;
        for (int i = 0; i < s.length(); i++) {
            sum += (s.charAt(i) - '0') * (s.charAt(i) - '0');
        }
        return sum;
    }

    public boolean isHappy(int n) {
        Set<Integer> set = new HashSet<>();
        set.add(n);
        while (n != 1) {
            n = calc(n);
            if(set.contains(n)){
                break;
            }else{
                set.add(n);
            }
        }
        return n == 1;
    }
}
